printing issue

by: Quillaur, 8 years ago


I have the following code :

from nltk.corpus import wordnet

fobj = open("the_path_of_my_file/test.txt", "r")
synonyms = []
for line in fobj:
    for syn in wordnet.synsets(line):
        for l in syn.lemmas():
            synonyms.append(l.name())
    print(line, end='')
    print(set(synonyms))


The file "test.txt" contains the following words :
dog
cat

When I run it, I get :
dog
set()
cat{'honk', 'hombre', 'CT', 'spew', 'computed_axial_tomography', 'big_cat', 'true_cat', 'retch', 'purge', 'chuck', 'barf', 'kat', "cat-o'-nine-tails", 'sick', 'be_sick', 'Caterpillar', 'puke', 'regorge', 'vomit_up', 'Arabian_tea', 'qat', 'computerized_axial_tomography', 'guy', 'cat', 'throw_up', 'quat', 'regurgitate', 'spue', 'computerized_tomography', 'upchuck', 'vomit', 'disgorge', 'cast', 'bozo', 'computed_tomography', 'African_tea', 'khat', 'CAT'}

But the set() should contain the synonyms of dog. I don't understand why I get this result. Anyone has an idea?

Thank you very much for your time.
Sincerely,
Quillaur.



You must be logged in to post. Please login or register an account.



I finally found the solution. For those who are interested, here it is :

from nltk.corpus import wordnet

fobj = open("C:/Users/omicX1/Documents/python_scripts/test.txt", "r")
synonyms = []

data = fobj.read()
my_list = data.splitlines()
my_list = [x.strip(' ') for x in my_list]

print(my_list)

for x in my_list:
    #line = [line.rstrip('n')]
    for syn in wordnet.synsets(x):
        for l in syn.lemmas():
            synonyms.append(l.name())
    print(x, end="n")
    print(set(synonyms))



-Quillaur 8 years ago

You must be logged in to post. Please login or register an account.